package com.atguigu.gulimall.search.controller;


import com.alibaba.fastjson.JSON;
import com.atguigu.gulimall.search.confing.GulimallElasticSearchConfig;
import com.atguigu.gulimall.search.entity.User;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
public class Txuejian {


    @Resource
    private RestHighLevelClient restHighLevelClient;

@GetMapping("/testsearch")
public  void  searchData() throws  IOException
{
    //创建索引请求
    SearchRequest searchResult = new SearchRequest();
    //指定索引
    searchResult.indices("bank");
    //指定DSL检索条件
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

//构造检索条件
    //按照年龄的分布进行聚合

    searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill"));

        TermsAggregationBuilder ageAgg= AggregationBuilders.terms("ageAgg").field("age").size(10);

    searchSourceBuilder.aggregation(ageAgg);
//
    AvgAggregationBuilder balance=  AggregationBuilders.avg("balanceAvg").field("balance");

    searchSourceBuilder.aggregation(balance);

    System.out.println("检索条件"+searchSourceBuilder.toString());

    searchResult.source(searchSourceBuilder);

    SearchResponse searchResponse = restHighLevelClient.search(searchResult,GulimallElasticSearchConfig.COMMON_OPTIONS);
    System.out.println("分析结果"+searchResponse.toString());

    Map map = JSON.parseObject(searchResponse.toString(), Map.class);

    System.out.printf("" + map);
    SearchHits hits = searchResponse.getHits();

    SearchHit[] searchHits = hits.getHits();
    for (SearchHit hit :searchHits) {
        hit.getIndex();
        hit.getScore();
    }

}

    @GetMapping("/testvv")
    public  String  testname()
    {
        HashMap<String, String> hashMap = new HashMap<String, String>();
       // System.out.println(restHighLevelClient);
        //测试存储数据
        IndexRequest indexRequest = new IndexRequest("users");
        indexRequest.id("1");
        User user = new User();
        user.setAge("19");
        user.setName("xuejian");
        user.setSex("nan");
        String jsonstring =JSON.toJSONString(user);
        System.out.println("userjson字符串"+jsonstring);
     //   indexRequest.source();
        //indexRequest.source("username", "zhangsan", "age", 18, "gender", "男");
       indexRequest.source(jsonstring,XContentType.JSON);
        IndexResponse indexRequest1=null;
        try {
            System.out.println("************");
        indexRequest1=restHighLevelClient.index(indexRequest,GulimallElasticSearchConfig.COMMON_OPTIONS);
        } catch (Exception ex ){
            System.out.printf("es抛出了异常"+ex.getMessage());
        }
        return "xuejian"+indexRequest1;
    }


}
